<template>
  <view class="login">
    <view class="logo"></view>
    <view class="content">
      <view class="contentText">
        <text>离院服务</text>
      </view>
      <view :class="switchover ? 'studentLabel' : 'teacherLabel'">
        <text
          class="student"
          @tap="studentClick"
        >账号登陆</text>
        <text
          class="teacher"
          @tap="teacherClick"
        >统一身份认证登录</text>
      </view>
      <view class="cutOffRule"></view>
      <view
        class="form"
        v-if="switchover"
      >
        <form @submit="formSubmit">
          <view class="username">
            <!-- 姓名图标 -->
            <view class="usernameIcon">姓名</view>
            <input
              type="text"
              placeholder="请输入真实姓名"
              v-model="username"
            />
          </view>
          <view class="studentNumber">
            <!-- 学号图标 -->
            <view class="studentNumberIcon">学号</view>
            <input
              type="text"
              placeholder="请输入学号"
              v-model="studentNumber"
            />
          </view>

          <view class="IDNumber">
            <!-- 证件号码图标 -->
            <view class="IDNumberIcon"> 身份证号</view>
            <input
              type="text"
              placeholder="请输入身份证号"
              v-model="IDNumber"
            />
          </view>
          <view class="hint">港澳台学生使用通行证号</view>
          <view class="btn">
            <button form-type="submit">登录</button>
          </view>
        </form>
      </view>
      <view
        class="form"
        v-else
      >
        <form @submit="formSubmit">
          <view class="studentNumber">
            <!-- 账号图标 -->
            <view class="accountIcon"> </view>
            <input
              type="text"
              placeholder="姓名"
              v-model="account"
            />
          </view>
          <view class="username">
            <!-- 密码图标 -->
            <view class="passwordIcon"></view>
            <input
              type="password"
              placeholder="密码"
              v-model="password"
            />
          </view>

          <view class="btn btns">
            <button form-type="submit">登录</button>
          </view>
        </form>
      </view>
    </view>
  </view>
</template>

<!-- <script lang="ts" setup> -->

<script>

import "./login.scss"
import Taro from "@tarojs/taro";
// import { Toast } from '@nutui/nutui-taro';
import { getCurrentInstance } from "vue";

export default {
  data() {
    return {
      switchover: true,
      studentNumber: '',
      username: '',
      IDNumber: '',
      account: '',
      password: '',
      loginInfo: {}
    }
  },
  methods: {
    studentClick() {
      this.switchover = true
    },
    teacherClick() {
      this.switchover = false
    },
    formSubmits() {
      if (this.switchover) {
        console.log(this.switchover)
        return
      }
    },
    formSubmit() {
      if (this.switchover) {
        if (this.studentNumber.trim() == '') {
          Taro.showToast({
            title: '学号不能为空',
            icon: 'none',
          })
          return
        }
        if (this.username.trim() == '') {
          Taro.showToast({
            title: '姓名不能为空',
            icon: 'none',
          })
          return
        }
        if (this.IDNumber.trim() == '') {
          Taro.showToast({
            title: '证件号码不能为空',
            icon: 'none',
          })
          return
        }
        this.login('1')
      } else {
        if (this.account.trim() == '') {
          Taro.showToast({
            title: '姓名不能为空',
            icon: 'none',
          })
          return
        }
        if (this.password.trim() == '') {
          Taro.showToast({
            title: '密码不能为空',
            icon: 'none',
          })
          return
        }
        this.login('0')
      }
    },

    //  onSubmit(values) {
    //     console.log('submit', values);
    //     this.login()  
    //   },
    async login(n) {
      let flora = this.flora
      const res = await Taro.login() //获取用户登录状态 //检测当前用户状态是否有效   可以获取opid

      if (n == '1') { //学生登录
        //  console.log("---++++++++++++=------",res.code)
        const r = await flora.users.studentlogin({  //学生登录

          code: this.studentNumber,   //学号
          name: this.username,    // 姓名
          idcard: this.IDNumber,  //身份证号码
          logincode: res.code,      //opid

          //  code: "student_0001",   //学号
          // name: "student_0001",    // 姓名
          // idcard:"340229199304239888",  //身份证号码
          // logincode: res.code,      //opid
        });
        if (r.status === 'ok') {
          // console.log("--------------",r)
          // Taro.showToast('登录成功', 'success')
          // Taro.showToast({
          //     title: '登录成功',
          //     icon: 'success',
          // })
          this.getUserInfo();
          // Taro.redirectTo({ url: '/pages/index/index' })
         // Taro.switchTab({ url: '/pages/index/index' })
          // const type = Taro.getStorageSync('type')

          return;
        }
        if (r.status === 'error') {
          // console.log("--------------",r)
          const errorMsg = JSON.parse(r.errorMsg)
          // Taro.showToast(errorMsg?.items?.[0]?.message,"error")
          // console.log("//////////",errorMsg.items[0].message)
          Taro.showToast({
            title: errorMsg.items[0].message,
            icon: 'none',
          })
        }
      } else if (n == '0') {
        // console.log("---++++++++++++=------",res.code)
        const r = await flora.users.teacherLogin({  //老师登录

          email: this.account, // 老师登陆的邮箱账号
          pwd: this.password,       //密码
          logincode: res.code, //opid

          //  email: "teacher_0002@flora.local", // 老师登陆的邮箱账号
          // pwd: "teacher_0002",       //密码
          // logincode: res.code, //opid
        });
        if (r.status === 'ok') {
          // console.log("--------------",r)
          // Taro.showToast({
          //     title: '登录成功',
          //     icon: 'success',
          // })
          this.getUserInfo();
        //  Taro.redirectTo({ url: '/pages/index/teacherIndex' })
          return;
        }
        if (r.status === 'error') {
          // console.log("--------------",r)
          const errorMsg = JSON.parse(r.errorMsg)
          // Taro.showToast(errorMsg?.items?.[0]?.message,"error")
          Taro.showToast({
            title: errorMsg.items[0].message,
            icon: 'none',
          })
        }
      }



    },

    getUserInfo() {

      let that = this
      const type = Taro.getStorageSync('type')
      // console.log("------------", type)

      const personid = Taro.getStorageSync('personid')
      // console.log("------------", personid)

      var modelneme = "";
      var fieldsname = [];
      if (type == "teacher") {
        fieldsname = ['id', 'name', 'email', 'phone', 'wechat_id.id', 'roles_ids.flora_external_id']
        modelneme = 'FreshTeacher'
      } else {
        fieldsname = ['id', 'code', 'name', 'name_middle', 'name_pin_yin', 'phone', 
        'email', 'id_card', 'birthday', 'age', 'sex', 'political_status_id.code',
         'political_status_id.name_lang_cn', 'wechat_id.id', 'dormitory_id.ascription','dormitory_id.name']
        modelneme = 'FreshStudent'
      }
      that.flora.dash.dataApi.search({
        vars: {
          fields: fieldsname,
          model: modelneme,
          condition: {
            logic_operator: '&',
            children: [
              {
                leaf: {
                  field: 'id',
                  comparator: '=',
                  value: personid,
                }
              },
            ]
          },
          limit: 10,
          offset: 0
        },
        cacheMode: 'no-cache',
      }).then((r) => {
        Taro.setStorageSync("userInfo", r.result.items[0].data)
         if (type == "teacher") {
            Taro.redirectTo({ url: '/pages/index/teacherIndex' })
         }else {
            Taro.redirectTo({ url: '/pages/index/index' })
         }
      })

    },
  }
}
</script>




